package com.czn.service.impl;

import com.czn.dao.DoctorDao;
import com.czn.dao.DrugsDao;
import com.czn.dao.IllnessDao;
import com.czn.dao.PatientDao;
import com.czn.entity.Doctor;
import com.czn.entity.Drugs;
import com.czn.entity.Illness;
import com.czn.entity.Patient;
import com.czn.service.IPatientService;
import com.czn.vo.ResponseResult;
import org.beetl.sql.core.engine.PageQuery;
import org.beetl.sql.core.query.LambdaQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ChenZeNan
 * @create 2021-04-17 15:30
 */
@Service
public class PatientServiceImpl implements IPatientService {
    @Autowired
    PatientDao patientDao;
    @Autowired
    DoctorDao doctorDao;
    @Autowired
    DrugsDao drugsDao;
    @Autowired
    IllnessDao illnessDao;

    /**
     * 分页/模糊查询医生所有记录
     *
     * @param pageNo
     * @param pageSize
     * @param name
     * @param certId
     * @return
     */
    @Override
    public PageQuery<Patient> findPatientList(long pageNo, long pageSize, String name, String certId) {
        PageQuery<Patient> query = new PageQuery<>(pageNo, pageSize);
        if (!StringUtils.isEmpty(name)) {
            query.setPara("name", "%" + name + "%");
        }
        if (!StringUtils.isEmpty(certId)) {
            query.setPara("certId", certId);
        }
        patientDao.selectList(query);
        return query;
    }

    /**
     * 新增患者
     *
     * @param patient
     * @return
     */
    @Override
    public ResponseResult addPatient(Patient patient) {

        ResponseResult result = new ResponseResult();
        // 根据身份证查询数据库中是否已有该对象了
        LambdaQuery<Patient> query = patientDao.createLambdaQuery();
        query.andEq(Patient::getCertId, patient.getCertId());
        Patient sysPatient = query.single();
        if (sysPatient == null) { // 数据库中没有该对象，直接插入
            patientDao.insert(patient);
            result.setCode("300");
            result.setMessage("信息添加成功！");
            return result;
        }
        // 数据库中已有该对象了
        result.setCode("301");
        result.setMessage("该病人信息已存在数据库中~");
        return result;
    }

    /**
     * 修改患者信息
     *
     * @param patient
     * @return
     */
    @Override
    public ResponseResult editPatient(Patient patient) {
        ResponseResult result = new ResponseResult();
        int rows = patientDao.updateTemplateById(patient);
        if (rows > 0) {
            result.setCode("500");
            result.setMessage("信息更新成功！");
            return result;
        }
        result.setCode("501");
        result.setMessage("信息更新失败！");
        return result;
    }

    /**
     * 根据记录id查询患者
     *
     * @param id
     * @return
     */
    @Override
    public Patient findPatientById(Integer id) {
        Patient patient = patientDao.single(id);
        return patient;
    }

    /**
     * 根据id删除患者
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deletePatientById(Integer id) {
        ResponseResult result = new ResponseResult();
        int rows = patientDao.deleteById(id);
        if (rows > 0) {
            result.setCode("400");
            result.setMessage("删除成功！");
            return result;
        }
        result.setCode("401");
        result.setMessage("删除失败，请重试！");
        return result;
    }

    /**
     * 查询所有的病人
     *
     * @return
     */
    @Override
    public List<Patient> findAllPatient() {
        List<Patient> patients = patientDao.all();
        return patients;
    }

    /**
     * 根据患者id查询患者
     *
     * @param id
     * @return
     */
    @Override
    public Patient findPatientByUserId(Integer id) {
        LambdaQuery<Patient> query = patientDao.createLambdaQuery();
        query.andEq("user_id", id);
        return query.single();
    }

    /**
     * 查询信息
     *
     * @param type
     * @param name
     * @return
     */
    @Override
    public Map<String, List> findInfo(String type, String name) {
        Map<String, List> map = new HashMap<>();
        List list = null;
        switch (type) {
            case "doctor":
                list = getDoctorList(name);
                map.put(type, list);
                break;
            case "illness":
                list = getIllnessList(name);
                map.put(type, list);
                break;
            case "drugs":
                list = getDrugsList(name);
                map.put(type, list);
                break;
            default:
                break;
        }
        return map;
    }

    /**
     * 查询医生信息
     *
     * @param name
     * @return
     */
    public List<Doctor> getDoctorList(String name) {
        LambdaQuery<Doctor> lambdaQuery = doctorDao.createLambdaQuery();
        if (!StringUtils.isEmpty(name)) {
            lambdaQuery.andLike(Doctor::getName, "%" + name + "%");
        }
        List<Doctor> doctors = lambdaQuery.select();
        return doctors;
    }

    /**
     * 查询疾病信息
     *
     * @param name
     * @return
     */
    public List<Illness> getIllnessList(String name) {
        LambdaQuery<Illness> lambdaQuery = illnessDao.createLambdaQuery();
        if (!StringUtils.isEmpty(name)) {
            lambdaQuery.andLike(Illness::getName, "%" + name + "%");
        }
        List<Illness> illnesses = lambdaQuery.select();
        return illnesses;
    }

    /**
     * 查询药品
     *
     * @param name
     * @return
     */
    public List<Drugs> getDrugsList(String name) {
        LambdaQuery<Drugs> lambdaQuery = drugsDao.createLambdaQuery();
        if (!StringUtils.isEmpty(name)) {
            lambdaQuery.andLike(Drugs::getName, "%" + name + "%");
        }
        List<Drugs> drugs = lambdaQuery.select();
        return drugs;
    }


}
